package ssi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import ssi.tools.Connector;


/**
 * Servlet implementation class InformationUserServlet
 */
@WebServlet("/InformationAnimalServlet")
public class InformationAnimalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		if (request.getParameter("userId") != null) {
			response.setContentType("application/json");      
			PrintWriter out = response.getWriter();
			int animal = Integer.parseInt(request.getParameter("userId"));
			try {
				Connection connection = new Connector().getConnection();
				Statement statement = connection.createStatement();
				ResultSet result = statement.executeQuery("SELECT * FROM `animals` WHERE id="+ animal);
				if (result.next()) {
					JSONObject jsonReturn = new JSONObject();
					jsonReturn.put("birthDate", result.getString("birthDate"));
					jsonReturn.put("gender", result.getString("gender"));
					jsonReturn.put("breed", result.getString("breed"));
					jsonReturn.put("name", result.getString("name"));
				
					out.print(jsonReturn);
					out.flush();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
}



































